package com.tcu.order.controller;

import com.tcu.exception.SmartException;
import com.tcu.order.entity.OrderStatus;
import com.tcu.order.service.IOrderStatusService;
import com.tcu.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/orderStatus")
public class OrderStatusController {
    private IOrderStatusService orderStatusService;

    @Autowired
    public void setOrderStatusService(IOrderStatusService orderStatusService) {
        this.orderStatusService = orderStatusService;
    }
    @PostMapping("add")
    public R add(@RequestBody OrderStatus orderStatus){
        if(orderStatus.getId()!=null){
            throw new SmartException(200001,"增加时，id必须为空");
        }
        boolean save = orderStatusService.save(orderStatus);
        return save ? R.success() : R.error();
    }
    @DeleteMapping("delete/{id}")
    public R delete(@PathVariable String id){
        boolean b = orderStatusService.removeById(id);
        return b ? R.success() : R.error();
    }
    @PostMapping("update")
    public R update(@RequestBody OrderStatus status){
        if(status.getId()==null){
            throw new SmartException(20001,"id为null，修改失败！");
        }
        boolean b = orderStatusService.updateById(status);
        return b ? R.success() : R.error();
    }
    @GetMapping("getOne/{id}")
    public R getOne(@PathVariable String id){
        OrderStatus byId = orderStatusService.getById(id);
        return R.success().data("orderStatus", byId);
    }
    @GetMapping("list")
    public R list(){
        List<OrderStatus> list = orderStatusService.list();
        return R.success().data("list", list);
    }
}
